package progetto.visualizzazione;

import grafi.Arco;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import socialnetworks.GrafoYouTube;
import socialnetworks.YouTubeDataExtractor;

import com.google.gdata.util.ServiceException;

public class SimulazioneGrafoMisureVale {
	
	static final int numeroNodi = 5;
	static GrafoYouTube grafoYouTube;
	static ArrayList <Double> pageRank;
	static ArrayList <Double> camminiMinimi;
	static final String delimitatore = "::";
	
	public static ArrayList <Double> getPageRank (){
		ArrayList <Double> pager =new ArrayList <Double> ();
		for(int i=0;i<numeroNodi;i++)
			pager.add(Math.random());
		return pager;
	}
	
	public static ArrayList <Double> getCamminiMinimi (){
		ArrayList <Double> camminim =new ArrayList <Double> ();
		for(int i=0;i<numeroNodi;i++)
			camminim.add(Math.random());
		return camminim;
	}
	
	public static GrafoYouTube getGrafoYouTube()  throws ServiceException, IOException{
		YouTubeDataExtractor extr=new YouTubeDataExtractor();
		List<String> seedIds=extr.query("android");		
		//List<String> seedIds=new ArrayList<String>();
		//seedIds.add("iSph2I-LLKs");
		GrafoYouTube g=new GrafoYouTube(numeroNodi,seedIds);
		/*Iterator<Arco> it=g.archi();
		while(it.hasNext())
			System.out.println(it.next());
		String titolo="";
		for(int i=0;i<numeroNodi;i++)
		{	String id=g.id(i);
			if(id.startsWith("VID::")){
				titolo=extr.titolo(id.substring(5));
				System.out.println(titolo);
			}
		}*/
		
		return g;
	}
	
	public static boolean scrivi () throws ServiceException{
		
		try {
		      FileOutputStream file = new FileOutputStream("file.txt");
		      PrintStream Output = new PrintStream(file);
		      Output.println(numeroNodi);
		      Output.println(delimitatore);
		      
		      scriviArchi(Output);		      
		      Output.println(delimitatore);

		      scriviNodi(Output);
		      
			  Output.close();
		      System.out.println("scrittura terminata con successo");
		      return true;
		} catch (IOException e) {
		      System.out.println("Errore: " + e);
		      System.exit(1);
		    }
		return false;
	}
	
	private static void scriviNodi(PrintStream output) throws ServiceException, IOException {
		String titolo="";
		YouTubeDataExtractor extr=new YouTubeDataExtractor();
		for(int i=0;i<numeroNodi;i++){	
			String id=grafoYouTube.id(i);
			
			titolo=extr.titolo(id.substring(5));
			output.println(id);
			
		}
		
		// TODO Auto-generated method stub
		
	}
	public static  ArrayList<String> readFromFile(String nomeFile) throws IOException {
		 ArrayList<String> result=new ArrayList<String>();

		   if (nomeFile != null) {
		       File f = new File(nomeFile);
		       BufferedReader reader = null;
		       if (f.exists()) {
		           try {
		              reader = new BufferedReader(new FileReader(f));
		           
		              String tmp = null;
		              while((tmp = reader.readLine()) != null) {
		                  result.add(tmp);
		              }
		          
		           } finally {
		              if(reader != null)
		                  reader.close();
		           }
		       }
		   }
		   return result;
		}



	public static void scriviArchi(PrintStream output){
		Iterator<Arco> it=grafoYouTube.archi();
		while(it.hasNext()){
	    	  Arco a=it.next();
	    	  output.println(a.getIn()+","+a.getFin());
			//output.flush();
			}
	}
	
	public static void main(String[] args) throws ServiceException, IOException
	{	
		grafoYouTube=getGrafoYouTube();
		pageRank=getPageRank();
		camminiMinimi=getCamminiMinimi();
		scrivi();
		List <String>l=readFromFile("file.txt");
           for(String s: l)
			System.out.println("Lettura da file "+s);
		
	}

}
